package com.shengqiang.cloud.oauth2.service;

import com.google.common.collect.Lists;
import com.shengqiang.cloud.oauth2.model.ProtectResource;
import com.shengqiang.cloud.oauth2.model.ResourceScope;
import com.shengqiang.cloud.oauth2.model.ResponseMsg;
import com.shengqiang.cloud.oauth2.repository.OAuth2ResourceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/12/11.
 * app 受保护资源管理操作
 */
@Service
public class OAuthAppResourceService {

    @Autowired
    private OAuth2ResourceDao auth2ResourceDao;

    /**
     * 注册资源信息
     *
     * @param resource
     * @return
     */
    public ResponseMsg<String> registerResource(ProtectResource resource) {
        ProtectResource hasResource = auth2ResourceDao.getProtectResource(resource.getResourceId());
        if (hasResource == null) {
            resource.setResourceUid(UUID.randomUUID().toString());
            boolean flag = auth2ResourceDao.saveAppResource(resource);
            if (flag) {
                return new ResponseMsg<>(1, "success");
            } else {
                return new ResponseMsg<>(0, "fail");
            }
        } else {
            return new ResponseMsg<>(2, "该资源已被注册,请不要重复注册!");
        }
    }

    /**
     * 更新资源信息
     *
     * @param resource
     * @return
     */
    public ResponseMsg<String> updateResource(ProtectResource resource) {
        ProtectResource hasResource = auth2ResourceDao.getProtectResource(resource.getResourceId());
        if (hasResource == null) {
            return new ResponseMsg<>(2, "该资源信息没有注册,请先注册!");
        } else {
            boolean flag = auth2ResourceDao.saveAppResource(resource);
            if (flag) {
                return new ResponseMsg<>(1, "success");
            } else {
                return new ResponseMsg<>(0, "fail");
            }
        }
    }

    /**
     * 查询受保护的资源列表
     *
     * @return
     */
    public ResponseMsg<List<ProtectResource>> queryResources() {
        List<ProtectResource> resources = auth2ResourceDao.queryProtectResources();
        if (resources == null) {
            return new ResponseMsg<List<ProtectResource>>(0, "fail", Lists.newArrayList());
        } else {
            return new ResponseMsg<List<ProtectResource>>(1, "success", resources);
        }
    }

    /**
     * 删除资源
     *
     * @param resourceId
     * @return
     */
    public ResponseMsg<String> deleteResource(String resourceId) {
        boolean flag = auth2ResourceDao.deleteProtectResource(resourceId);
        if (flag) {
            return new ResponseMsg<String>(1, "success");
        } else {
            return new ResponseMsg<>(0, "fail");
        }
    }

    /**
     * 添加资源的scope
     *
     * @param resourceId
     * @param scope
     * @return
     */
    public ResponseMsg<String> addResourceScope(String resourceId, ResourceScope scope) {
        ProtectResource resource = auth2ResourceDao.getProtectResource(resourceId);
        if (resource == null) {
            return new ResponseMsg<>(2, "找不到资源的注册信息,请查看资源");
        } else {
            resource.getScopes().add(scope);
            boolean flag = auth2ResourceDao.saveAppResource(resource);
            if (flag) {
                return new ResponseMsg<>(1, "success");
            } else {
                return new ResponseMsg<>(0, "fail");
            }
        }
    }

    /**
     * 更新Scope信息
     *
     * @param resourceId
     * @param scope
     * @return
     */
    public ResponseMsg<String> updateResourceScope(String resourceId, ResourceScope scope) {
        ProtectResource resource = auth2ResourceDao.getProtectResource(resourceId);
        if (resource == null) {
            return new ResponseMsg<>(2, "找不到资源的注册信息,请查看资源");
        } else {
            List<ResourceScope> scopes = resource.getScopes();
            if (scopes != null && scopes.size() > 0) {
                for (ResourceScope rScope : scopes) {
                    if (rScope.getScopeUid().equals(scope.getScopeUid())) {
                        scopes.remove(rScope);
                        break;
                    }
                }
                scopes.add(scope);
                resource.setScopes(scopes);
                boolean flag = auth2ResourceDao.saveAppResource(resource);
                if (flag) {
                    return new ResponseMsg<>(1, "success");
                } else {
                    return new ResponseMsg<>(0, "fail");
                }
            } else {
                return new ResponseMsg<>(2, "找不到需要修改的信息");
            }
        }
    }

    /**
     * 删除指定的scope
     * @param resourceId
     * @param scopeId
     * @return
     */
    public ResponseMsg<String> deleteScope(String resourceId, String scopeId) {
        ProtectResource resource = auth2ResourceDao.getProtectResource(resourceId);
        if (resource == null) {
            return new ResponseMsg<>(2, "找不到资源的注册信息,请查看资源");
        } else {
            List<ResourceScope> scopes = resource.getScopes();
            if (scopes != null && scopes.size() > 0) {
                for (ResourceScope rScope : scopes) {
                    if (rScope.getScopeUid().equals(scopeId)) {
                        scopes.remove(rScope);
                        break;
                    }
                }
                resource.setScopes(scopes);
                boolean flag = auth2ResourceDao.saveAppResource(resource);
                if (flag) {
                    return new ResponseMsg<>(1, "success");
                } else {
                    return new ResponseMsg<>(0, "fail");
                }
            } else {
                return new ResponseMsg<>(2, "找不到需要删除的信息");
            }
        }
    }
}
